AudioBuffer = newFunc('AudioBuffer', 1, function AudioBuffer(ctx) {
    ctx['channel'] = []
    mm.memory.private_data.set(this, ctx)
})


Object.defineProperties(AudioBuffer.prototype, {
    length: {

        get: newFunc('length', 10, function length() {
            let res = mm.memory.private_data.get(this).length;
            if(debug) print('AudioBuffer.length.get', res)

            return res
        }, {get: true}), enumerable: true, configurable: true,

    },
    duration: {

        get: newFunc('duration', 10, function duration() {
            let res = mm.memory.private_data.get(this).duration;
            if(debug) print('AudioBuffer.duration.get', res)

            return res
        }, {get: true}), enumerable: true, configurable: true,

    },
    sampleRate: {

        get: newFunc('sampleRate', 10, function sampleRate() {
            let res = mm.memory.private_data.get(this).sampleRate;
            if(debug) print('AudioBuffer.sampleRate.get', res)

            return res
        }, {get: true}), enumerable: true, configurable: true,

    },
    numberOfChannels: {

        get: newFunc('numberOfChannels', 10, function numberOfChannels() {
            let res = mm.memory.private_data.get(this).numberOfChannels;
            if(debug) print('AudioBuffer.numberOfChannels.get', res)

            return res
        }, {get: true}), enumerable: true, configurable: true,

    },
    copyFromChannel: {

        value: newFunc('copyFromChannel', 10, function copyFromChannel() {
            if(debug) print('AudioBuffer.copyFromChannel.call', arguments)

        }), writable: true, enumerable: true, configurable: true,


    },
    copyToChannel: {

        value: newFunc('copyToChannel', 10, function copyToChannel() {
            if(debug) print('AudioBuffer.copyToChannel.call', arguments)

        }), writable: true, enumerable: true, configurable: true,


    },
    getChannelData: {

        value: newFunc('getChannelData', 10, function getChannelData(v) {
            // let res = new Float32Array(mm.memory.private_data.get(this).length);
            let _channel = mm.memory.private_data.get(this).channel
            if(!_channel[v]) {
                let res =  new Float32Array(mm.memory.private_data.get(this).length);
                for (let i = 0; i < res.length; i++) {
                    res[i] = Math.random() - 0.5
                }
                _channel[v] = res

            }
            let res = _channel[v]
            if(debug) print('AudioBuffer.getChannelData.call', arguments)


            return res;

        }), writable: true, enumerable: true, configurable: true,


    },
'constructor': {

            value: newFunc('constructor', 10, function () {

                      if (!AudioBuffer.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('AudioBuffer.constructor.call', arguments)

            }), writable:true, enumerable:false, configurable:true,



        },
})


OfflineAudioContext = newFunc('OfflineAudioContext', 3, function OfflineAudioContext(numberOfChannels, length, sampleRate) {
    if(debug) print('OfflineAudioContext.new', numberOfChannels, length, sampleRate)
    let dest = new AudioDestinationNode({
        channelCount: 2,
        channelCountMode: "explicit",
        channelInterpretation: "speakers",
        context: this,
        maxChannelCount: 2,
        numberOfInputs: 1,
        numberOfOutputs: 0,
    });
    dest = mm.proxy(dest, 'AudioDestinationNode')
    let config = {
        numberOfChannels: numberOfChannels, length: length, sampleRate: sampleRate,
        oncomplete: null,
        baseTime: Date.now(),
        destination: dest
    }
    mm.memory.private_data.set(this, config)
    BaseAudioContext.call(this, {numberOfChannels, length, sampleRate})
})




; delete OfflineAudioContext.prototype.constructor;
;
Object.defineProperties(OfflineAudioContext.prototype, {
    [Symbol.toStringTag]:{
        value:'OfflineAudioContext'
    },

'oncomplete': {

                  get: newFunc('oncomplete', 10, function (){


                    if (!OfflineAudioContext.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['oncomplete'];
            if (debug)print('OfflineAudioContext.oncomplete.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,


             set: newFunc('oncomplete', 1, function (value){
                if (debug)print('OfflineAudioContext.oncomplete.set', value)
            mm.memory.private_data.get(this)['oncomplete'] = value

            }, {set:true}),  enumerable:true, configurable:true,



        },
'length': {

                  get: newFunc('length', 10, function (){


                    if (!OfflineAudioContext.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;

            let res =  mm.memory.private_data.get(this)['length'];
            if (debug)print('OfflineAudioContext.length.get', res)

            return res
        }, {get: true}), enumerable:true, configurable:true,

        },
'resume': {

            value: newFunc('resume', 10, function () {

                      if (!OfflineAudioContext.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('OfflineAudioContext.resume.call', arguments)

            }), writable:true, enumerable:true, configurable:true,



        },
'startRendering': {

            value: newFunc('startRendering', 10, function () {

                      if (!OfflineAudioContext.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('OfflineAudioContext.startRendering.call', arguments)

            }), writable:true, enumerable:true, configurable:true,



        },
'suspend': {

            value: newFunc('suspend', 10, function () {

                      if (!OfflineAudioContext.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('OfflineAudioContext.suspend.call', arguments)

            }), writable:true, enumerable:true, configurable:true,



        },
'constructor': {

            value: newFunc('constructor', 10, function () {

                      if (!OfflineAudioContext.prototype.isPrototypeOf(this)) {
                throw new TypeError("Illegal constructor");
            }
            ;
                    if (debug)print('OfflineAudioContext.constructor.call', arguments)

            }), writable:true, enumerable:false, configurable:true,



        },
})
Object.setPrototypeOf(OfflineAudioContext.prototype, BaseAudioContext.prototype)
Object.setPrototypeOf(OfflineAudioContext, BaseAudioContext)
